<?php

namespace app\merch\controller;

use app\commons\model\Member;
use app\merch\controller\Main;
use dousdk\AuthAppDouyin;
use douyin\Dy;
use think\Controller;
use think\Db;

class Videom extends Main {

    public function index() {

        $_GPC = input();
        $page = max(intval($_GPC['page']), 1);
        $limit = max(intval($_GPC['limit']), 10);
        $row = Db::name('member_video')
            ->alias('v')
            ->leftJoin('sale s', 'v.sale_id=s.id')
            ->leftJoin('member m', 'v.member_id=m.id')
            ->field('v.*, s.title, s.poi_name ,m.nickname,m.mobile');
        $list = \app\commons\model\Sale::where(['member_id'=>$this->merchinfo['id']])->select();
        $ids = [0];
        if(!empty($list)){
            foreach ($list as $item){
                $ids[] = $item['id'];
            }
        }
        $row->where('v.sale_id','in',$ids);
        if (isset($_GPC['title']) && $_GPC['title'] != '') {
            $row = $row->whereLike('s.title', "%{$_GPC['title']}%");
        }
        if (isset($_GPC['mobile']) && $_GPC['mobile'] != '') {
            $row = $row->whereLike('m.mobile', "%{$_GPC['mobile']}%");
        }
        $count = $row->count();
        $data = $row->page($page, $limit)->order('createtime', 'DESC')->select();
//        $data = $row->paginate($page);
        return jsonpp($data,$count);
    }

    public function getdata() {

        $_GPC = input();
        $id = $_GPC['id'];
        $data = Db::name('member_video')->where('id','in',$id)->field(['item_id','member_id','id'])->select();
        $errNum = 0;
        $num = 0;
        if(!empty($data)){
            foreach ($data as $datum){
                $member = Member::where(['id'=>$datum['member_id']])->field(['openid','access_token'])->find();
                $udata = array('status' => 0, 'updatetime' => time());
                $vdata = Dy::syncVideo($member['openid'],$member['access_token'],$datum['item_id']);
                if ($vdata['extra']['error_code'] == 0) {
                    $udata['avg_play_duration'] = $vdata['data']['result']['avg_play_duration'];
                    $udata['total_comment'] = $vdata['data']['result']['total_comment'];
                    $udata['total_like'] = $vdata['data']['result']['total_like'];
                    $udata['total_play'] = $vdata['data']['result']['total_play'];
                    $udata['total_share'] = $vdata['data']['result']['total_share'];
                    $udata['status'] = 1;
                }
                $res = Db::name('member_video')->where(['id'=>$datum['id']])->update($udata);
                if(!$res){
                    $errNum+=1;
                }else{
                    $num+=1;
                }
            }
        }
        return jsont([],0,'更新成功！');
        $_GPC = $this->data;
        $id = $_GPC['id'];
        $data = pdo_get('member_video', array('id' => $id));
        $member = json_decode($data['member'], true);
        $udata = array('status' => 0, 'updatetime' => time());

        $sysset = get_sysset();

        $dy = new AuthAppDouyin;
        $dy->client_key = $sysset['basic']['client_key'];
        $dy->client_secret = $sysset['basic']['client_secret'];

        $vdata=$dy->get_video_info($member['access_token'],$member['openid'] ,$data['item_id']);

        if ($vdata['error_code'] == 0) {
            $udata['avg_play_duration'] = $vdata['result']['avg_play_duration'];
            $udata['total_comment'] = $vdata['result']['total_comment'];
            $udata['total_like'] = $vdata['result']['total_like'];
            $udata['total_play'] = $vdata['result']['total_play'];
            $udata['total_share'] = $vdata['result']['total_share'];
            $udata['status'] = 1;
        }
        pdo_update('member_video', $udata, array('id' => $id));
        jsonp([]);
    }

    public function del() {

        $_GPC = input();
        $id = $_GPC['id'];
        if ($this->request->isPost()) {
            //删除用户数据
            $result = Db::name('member_video')->delete($id);
            if($result){
                jsont([],0,'删除成功！');
            }
            jsont([], 1, '删除失败');
        }
        jsont([], 1, 'error');
    }

}
